<template>
  <div class="report-form-content">
    <div class="tabs-form-head">
      <span>合同中止-基本信息</span>
      <div class="">
        <el-button type="primary" plain @click="returnBtn">返回</el-button>
      </div>
    </div>
    <el-divider border-style="dashed" />
    <el-row :gutter="20">
      <el-col :sm="24" :lg="24" style="padding-left: 20px" v-if="objList.length === 0">
        <el-card class="update-log">
          <el-empty :image-size="100" />
        </el-card>
      </el-col>
      <el-col :sm="24" :lg="24" style="padding-left: 20px" v-else>
        <el-card class="update-log">
          <div class="report-form-list-content">
            <div class="report-list">
              <span>中止经办人：</span>
              <span>{{ showIdLabel(contractSignerList, obj.interruptUserId, 'name') }}</span>
            </div>
            <div class="report-list">
              <span>中止经办部门：</span>
              <span>{{ showIdLabel(organizationList, obj.interruptDepartmentId, 'organizationName') }}</span>
            </div>
          </div>
          <div class="report-form-list-content-1">
            <div class="report-list">
              <span>中止原因：</span>
              <span>{{ obj.interruptReason }}</span>
            </div>
          </div>
        </el-card>
      </el-col>
    </el-row>
    <div class="tabs-form-head" v-if="objList.length > 0">
      <span>合同中止-附件</span>
    </div>
    <el-row :gutter="20"  v-if="objList.length > 0">
      <el-col :sm="24" :lg="24" style="padding-left: 20px">
        <el-card class="update-log">
            <el-table :data="oFiles"
              style="width: 100%; margin-top: 20px"
              row-key="id"
              border
              default-expand-all
              @selection-change="handleSelectionChange"
            >
              <el-table-column type="selection" width="55" />
              <el-table-column label="附件类型">
                <template v-slot="{row}">
                  {{ row.attachmentType }}
                </template>
              </el-table-column>
              <el-table-column prop="attachmentName" label="附件名称" />
              <el-table-column align="center" label="操作" width="160px" fixed="right">
                <template #default="scope">
                  <el-button type="primary" link @click="downloadFile1(scope.row)">下载</el-button>
                  <el-button type="primary" link @click="viewFile1(scope.row)">预览</el-button>
                </template>
              </el-table-column>
            </el-table>
        </el-card>
      </el-col>
    </el-row>
    <el-dialog v-model="attachmentVisible" title="附件预览" width="70%" top="2vh">
      <attachmentPreview v-model:attachmentId="previewAttachmentId" />
      <div style="text-align:right;">
        <el-button type="danger" @click="attachmentVisible = false">关闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script setup>
import {ref, getCurrentInstance} from 'vue'
import {doGet, doDownload} from "@/config/http";
import {svc, showIdLabel, getListPageSize} from "@/config/common";
import attachmentPreview from '@/views/components/base/attachmentPreview.vue'
import { useRoute, useRouter } from 'vue-router'
import initListStore from '@/store/modules/initList'

const router = useRouter()
const route = useRoute()

const { proxy: ctx } = getCurrentInstance();

let obj = ref({
  contractInitialId: route.query.id, // 	合同ID
  createBy: '', // 创建人用户ID
  createTime: '',// 创建时间
  id: '', //  主键ID
  interruptDepartmentId: '', // 中止经办人部门
  interruptReason: '', // 中止原因
  interruptUserId: '', // 中止经办人
  updateBy: '', // 最后修改时间
  version: '', // 数据版本
})

let objList = ref([])

let attachmentVisible = ref(false)
let previewAttachmentId = ref('')
let oFiles = ref([])

// let contractTypeList = ref([]) // 合同类型
// let contractCooperationList = ref([]) // 往来单位
// let contractFeetypeList = ref([]) // 合同费用类型
// let contractTemplateList = ref([]) // 合同模版
// let bankList = ref([]) // 银行
// let userList = ref([]) // 用户
// let governmentList = ref([]) //  政府单位
let organizationList = initListStore().organizationList // 部门
let contractSignerList = initListStore().contractSignerList // 签订人
// let contractFiletypeList = ref([]) // 合同附件类型
// const initList = async () => {
//   let p1 = doGet(svc, '/contract-type/page', {pageSize: getListPageSize()}, false) // 合同类型
//   let p2 = doGet(svc, '/contract-cooperation/page', {pageSize: getListPageSize()}, false) // 往来单位
//   let p3 = doGet(svc, '/contract-feetype/page', {pageSize: getListPageSize()}, false) // 合同费用类型
//   let p4 = doGet(svc, '/contract-template/page', {pageSize: getListPageSize()}, false) // 合同模版
//   let p5 = doGet(svc, '/bank/page', {pageSize: getListPageSize()}, false) // 银行
//   let p6 = doGet(svc, '/user/page', {pageSize: getListPageSize()}, false) // 用户
//   let p7 = doGet(svc, '/government/page', {pageSize: getListPageSize()}, false) //  政府单位
//   let p8 = doGet(svc, '/organization/page', {pageSize: getListPageSize()}, false) // 部门
//   let p9 = doGet(svc, '/contract-signer/page', {pageSize: getListPageSize()}, false) // 签订人
//   let p10 = doGet(svc, '/contract-filetype/page', {pageSize: getListPageSize()}, false) // 合同附件类型

//   let resArray = await Promise.all([p1, p2, p3, p4, p5, p6, p7, p8, p9, p10])
//   contractTypeList.value = resArray[0]['records']
//   contractCooperationList.value = resArray[1]['records']
//   contractFeetypeList.value = resArray[2]['records']
//   contractTemplateList.value = resArray[3]['records']
//   bankList.value = resArray[4]['records']
//   userList.value = resArray[5]['records']
//   governmentList.value = resArray[6]['records']
//   organizationList.value = resArray[7]['records']
//   contractSignerList.value = resArray[8]['records']
//   contractFiletypeList.value = resArray[9]['records']
// }

const initObj = async () =>  {
  if (route.query.id) {
    objList.value = await doGet(svc, '/contract-initial-interrupt/listByField', {field: 'contractInitialId', values: route.query.id}, true)
    if (objList.value.length > 0) {
      obj.value = objList.value[0]
    }
    oFiles.value = await doGet(svc, '/contract-initial-interrupt-attachment/listByField', {field: 'contractInitialInterruptId', values: obj.value.id}, false)
  }
}

// initList()
initObj()

const downloadFile1 = (row) => {
  console.log('row',row)
  if (row.attachmentId) {
    doDownload(svc, `/customAttachment/download/${row.attachmentId}`, {})
  } else {
    doDownload(svc, `/customAttachment/download/${row.id}`, {})
  }
}
const viewFile1 = (row) => {
  attachmentVisible.value = true
  setTimeout(() => {
    if (row.attachmentId) {
      previewAttachmentId.value = row.attachmentId
    } else {
      previewAttachmentId.value = row.id
    }
    console.debug('previewAttachmentId.value',previewAttachmentId.value)
  },100)
}

const returnBtn = () => {
  switch (route.query.kind) {
    case '1':
      ctx.goTargetByName('contractInitial', {});
      break;
    case '2':
      ctx.goTargetByName('contractMain', {})
      break;
  }
}
</script>
<style scoped>
.report-form-content {
  padding: 20px;
}
.update-log {
  width: 100%;
  margin: 30px 0;
}
.report-list {
  width: 31%;
  margin: 15px;
  display: flex;
  align-items: center;
}
.report-list span:nth-child(1) {
  display: inline-block;
  width: 120px;
  text-align: right;
}
.tabs-form-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.tabs-form-head span {
  font-size: 18px;
  color: #303133;
  font-weight: bold;
}
</style>
